<?php



/**
	 * Gets the color scheme of the activated theme
	 * @return string colors
*/
function getColors() {
	global $b;
	$colors = $b->getColors(get_attrib("colorscheme"));

	for ( $i = 0; $i < count($colors); $i++ ) {
		$color = explode("#", $colors[$i]);
		$c .= $color[1] . ",";
	}

	return $c;
}

/**
	 * Gets a attribute for the activated theme
	 * @param string $attrib the template attributes
	 * @return string
*/
function get_attrib($attrib) {
	global $b;
	$actTheme = $b->getActivatedThemeAttrib($b->getActivatedThemeID(), $attrib);

	return $actTheme;
}

/**
	 * Checks a attribute whether it is present or not
	 * @param string $attrib the template attribute
	 * @return boolean
*/
function check_attrib($attrib) {
	global $b;
	$actTheme = $b->getActivatedThemeAttrib($b->getActivatedThemeID(), $attrib);

	if($actTheme)
		return true;
	else
		return false;
}

/**
	 * Generates inline CSS for a theme based on template settings
	 * @param string $selector The css selector
	 * @param string $font The font for the selector
	 * @param string $size The font-size for the selector
 	 * @param string $color The font-color for the selector
	 * @return string
*/
function generateTextCSS($selector,$font,$size,$color) {
	if($font)
		$font = "font-family:$font;";

	if($size)
		$size = "font-size:$size%;";

	if($color)
		$color = "color:#$color;";

	return "$selector { $font $size $color }";
}


//dependency for writeCSS
function rglob($pattern='*', $flags = 0, $path='') {
    $paths = glob($path . '*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
    $files = glob($path . $pattern, $flags);
    foreach ( $paths as $path ) {
		$files = array_merge( $files, rglob($pattern, $flags, $path) );
	}
    return $files;
}


//$dir = header,footer,widget,content
//$o = select | link
function writeCSS($dir, $o = "select") {
	$files = rglob('*', 0, BP_REPO_ABSPATH . '/' . $dir . '/');

	foreach ( $files as $file ) {
	$lines = file($file);
		foreach ( $lines as $line_num => $line ) {
			$subject = $line;

			$pattern = "/" . $dir . "css/"; //make pattern for text as /textcss/

			//match the pattern with each line and store the matches
			preg_match($pattern, $subject, $matches);

			//if there is any match, then explode with : sperator so we can get the pseudo name for that file.css
			if ( $matches ) {
				$line = explode(' : ', $line);

				//if pseduo name exists then filen = pseudo name else the filename itself
				if($line[1])
					$filen = $line[1];
				else
					$filen = basename($file);

				//always make the default.css as default for every dir viz (footer,header,content,widget)
				if ( ($dir . '-' . basename($file)) == ($dir . "-default.css") )
					$selected = "selected";
				else
					$selected = "";

				//if select, then echo options
				if ( $o == "select" )
					echo '<option value="' . $dir . '-' . basename($file) . '" ' . $selected . '>' . $filen . '</option>';

				//if link then echo links to alternate stylesheets
				if ( $o == "link" )
					echo '<link rel="alternate stylesheet" type="text/css" href="' . BP_REPO_URL . '/' . $dir . '/' . basename($file) . '" title="' . $dir . '-' . basename($file) . '" media="screen" />' . "\n";
			} else {
				continue;
			}
		}
	}
}
